<template>
	<view class="content">
			<!-- 产品变更清单 -->
			<view class="plan" >
				<view class="grace-nowrap grace-flex-vcenter">
					<view class="hd-title"></view>
					<view style="font-size: 34rpx;">{{i18n[title] ? i18n[title] : title}}</view>
				</view>
				<!-- 列表展示 -->
				<view class="product" >
					<view class="product-content " v-for="(item, index) in list" :key="item.id">
						<!-- todo: 序号 -->
						<view class="" v-if="!isHideDefine9">
							<view class="product-title">{{i18n['Define9'] ? i18n['Define9'] : 'Define9'}}</view>
							<view class="left-margin">
								<input class="uni-input" type="text" placeholder="请输入" v-model="item.Define9" :disabled="isfiledDisabled.Define9"/>
							</view>
						</view>
						<view class="">
							<view class="product-title">{{i18n['cInvCode'] ? i18n['cInvCode'] : 'cInvCode'}}</view>
							<view class="left-margin">
								<input class="uni-input" type="text" placeholder="请输入" v-model="item.cInvCode" :disabled="isfiledDisabled.cInvCode"/>
							</view>
						</view>
						<view class="">
							<view class="product-title">{{i18n['cInvName'] ? i18n['cInvName'] : 'cInvName'}}</view>
							<view class="left-margin">
								<input class="uni-input" type="text" placeholder="请输入" v-model="item.cInvName" :disabled="isfiledDisabled.cInvName"/>
							</view>
						</view>
						<view class="">
							<view class="product-title">{{i18n['Define1'] ? i18n['Define1'] : 'Define1'}}</view>
							<view class="left-margin">
								<input class="uni-input" type="text" placeholder="请输入" v-model="item.Define1" :disabled="isfiledDisabled.Define1"/>
							</view>
						</view>
						<view class="">
							<view class="product-title">{{i18n['Define2'] ? i18n['Define2'] : 'Define2'}}</view>
							<view class="left-margin">
								<input class="uni-input" type="text" @change="calculate1(item, index)" placeholder="请输入" v-model="item.Define2" :disabled="isfiledDisabled.Define2"/>	
							</view>
						</view>
						<view class="">
							<view class="product-title">{{i18n['Define3'] ? i18n['Define3'] : 'Define3'}}</view>
							<view class="left-margin">
								<input class="uni-input" type="text" @change="calculate(item, index)" placeholder="请输入" v-model="item.Define3" :disabled="isfiledDisabled.Define3"/>	
							</view>
						</view>
						<view class="">
							<view class="product-title">{{i18n['Define4'] ? i18n['Define4'] : 'Define4'}}</view>
							<view class="left-margin">
								<input class="uni-input" placeholder="请输入" type="text" v-model="item.Define4" :disabled="isfiledDisabled.Define4"/>
							</view>
						</view>
						<view class="">
							<view class="product-title">{{i18n['Define5'] ? i18n['Define5'] : 'Define5'}}</view>
							<view class="left-margin">
								<input class="uni-input" type="text" placeholder="请输入" v-model="item.Define5" :disabled="isfiledDisabled.Define5"/>
							</view>
						</view>
						<view class="">
							<view class="product-title">{{i18n['Define6'] ? i18n['Define6'] : 'Define6'}}</view>
							<view class="left-margin">
								<input class="uni-input" type="text" placeholder="请输入" v-model="item.Define6" :disabled="isfiledDisabled.Define6"/>
							</view>
						</view>
						<view class="">
							<view class="product-title">{{i18n['Define8'] ? i18n['Define8'] : 'Define8'}}</view>
							<view class="left-margin">
								<input class="uni-input" type="text" placeholder="请输入" v-model="item.Define8" :disabled="isfiledDisabled.Define8"/>
							</view>
						</view>
						<view class="">
							<view class="product-title">{{i18n['Define10'] ? i18n['Define10'] : 'Define10'}}</view>
							<view class="left-margin">
								<input class="uni-input" type="text" placeholder="请输入" v-model="item.Define10" :disabled="isfiledDisabled.Define10"/>
							</view>
						</view>
						<view class="cancel" @click="del(item)" v-if="isShowToolBtn">
							<view class="icon iconfont icon-quxiao"></view>
						</view>
					</view>
				</view>
				<view class="add grace-nowrap" @click="addItem" v-if="isShowToolBtn">
					<view class="icon iconfont icon-tianjia" style="color: #00B26A; font-size: 42rpx;"></view>
					<span style='font-size: 32rpx;'>新增</span>
				</view>
			</view>
			<!-- 选择客户窗口 -->
			<uni-drawer ref="productChoose" mode="right" :width="300">
				<my-product-choose @confirm="addproduct"></my-product-choose>
			</uni-drawer>
			<view class="btn-box">
				<button type="default" @click="save">确定</button>
			</view>
	</view>
</template>

<script>
	import uniDrawer from "@/components/uni-drawer/uni-drawer.vue"
	import uniSearchBar from '@/components/uni-search-bar/uni-search-bar.vue'
	import myProductChoose from '@/components/my-product-choose.vue'
	import vvSelect from '@/components/vv-select/vv-select'
	import {ajax,getUserInfo} from '@/common/common.js'
	import {Url,bpmUrl} from "@/common/config.js"
	export default {
		data() {
			return {
				title: '',
				list: [],
				OldDepart: '',
				customerList:[],
				user: {},
				isShowToolBtn: false,
				isfiledDisabled: {},
				searchField: {
					paramasMap: {},
					headersMap: {
						sysid: "bpm",
						syspwd: "pass",
						userid: ''
					},
					url: ''
				},
				isHideDefine9: false
			}
		},
		components: {
			uniDrawer,
			uniSearchBar,
			myProductChoose,
			vvSelect
		},
		computed: {
			i18n() {
				return this.$t('form')
			}
		},
		onLoad(opt) {
			this.formData = JSON.parse(opt.formData) // 只读，仅注册可以不放在 data 管理 
			this.bussiness = JSON.parse(opt.bussiness) // 只读，仅注册可以不放在 data 管理 
			this.title = opt.title
			if (this.bussiness.currentNodeid=="T10002") {
				this.isShowToolBtn = true
			}
			this.formOnLoad()
		},
		methods: {
			// 加载表单
			async formOnLoad() {
				let DocUnid = this.bussiness.docUnid
				await ajax({
					url:'/bpm/post',
					method: 'POST',
					data: {
						...this.searchField, 
						...{
							paramasMap: {
								DocUnid
							},
							url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B159',
						} 
					},
					success: (res) => {
						// todo: 子表单初始赋值
						this.list = res.data.data
					}
				})
				this.initFiledState()
				let type = this.formData.Type
				if(type=="适用指定终端"){
					this.isHideDefine9 = true
				}else if(type=="适用全部终端"){
					this.isHideDefine9 = false
				}else{
					this.isHideDefine9 = true
				}
			},
			
			// 计算结算价折扣
			async calculate(item, index) {
				let Cpbj = item['Define1']
				let Jsjzk = item['Define3']
				let cost = Cpbj * Jsjzk / 100; // 计算出结算价
				this.list[index]['Define2'] = cost
				let DocUnid = this.bussiness.docUnid
				let type = ''
				let cInvCode = item['cInvCode']
				await ajax({
					url:'/bpm/post',
					method: 'POST',
					data: {
						...this.searchField, 
						...{
							paramasMap: {
								cInvCode,
								DocUnid,
								Define7: this.formData['Define7']
							},
							url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B166',
						} 
					},
					success: (res) => {
						type = res.data.data
					}
				})
				let Pmj = type
				let Pricevalue = item['Define2']
				if(Pmj==""){
					this.list[index]['Define4'] = '无票面价'
				}else{
					if(parseInt(Pmj)<parseInt(Pricevalue)){
							// 票面价(逻辑表)小于结算价时
							let define8 = item['Define8'] // 原票面价
							if(define8){//原票面价为空时
							this.list[index]['Define4'] = Pricevalue
						}else{
							let jsj = Pricevalue;
							let ypmj = define8;
							if(parseInt(jsj)>parseInt(ypmj)){
								this.list[index]['Define4'] = jsj
							}else{
								this.list[index]['Define4'] = define8
							}
						}
					}else{
						// 票面价(逻辑表)大于等于结算价时
						let define8 = item['Define8'] // 原票面价
						if(define8==""){//原票面价为空时
							this.list[index]['Define4'] = Pmj
						}else{
							if(Pmj>define8){
								this.list[index]['Define4'] = Pmj
							}else{
								this.list[index]['Define4'] = define8
							}
						}
					}
				}
				// 赋值完成票面价后计算票面价折扣
				let Pmjv = item['Define4']
				let Pmjzk = (Pmjv)/(Cpbj)*100;
				this.list[index]['Define5'] = Pmjzk
			},
			
			// 计算结算价
			async calculate1(item, index) {
				let Price = item['Define2']
				let Cpbj = item['Define1']
				let cost = Price / Cpbj * 100; // 计算出结算价
				this.list[index]['Define3'] = cost
				// 获取逻辑表票面价的值
				let DocUnid = this.bussiness.docUnid
				let type
				let cInvCode = item['cInvCode']
				await ajax({
					url:'/bpm/post',
					method: 'POST',
					data: {
						...this.searchField, 
						...{
							paramasMap: {
								cInvCode,
								DocUnid,
								Define7: this.formData['Define7']
							},
							url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B166',
						} 
					},
					success: (res) => {
						type = res.data.data
					}
				})
				let Pmj = type;
				let Pricevalue = Price;
				if(Pmj==""){
					this.list[index]['Define4'] = '无票面价'
				} else {
					if(parseInt(Pmj) < parseInt(Pricevalue)){
						// 票面价(逻辑表)小于结算价时
						let define8 = item['Define8'] // 原票面价
						if(define8=="") { // 原票面价为空时
							this.list[index]['Define4'] = 'Pricevalue'
						} else{
							if(Price > define8) {
									this.list[index]['Define4'] = Price
							} else {
								this.list[index]['Define4'] = define8
							}
						}
					} else {
						//票面价(逻辑表)大于等于结算价时
						let define8 = item['Define8'] // 原票面价
						if(define8) { // 原票面价为空时
								this.list[index]['Define4'] = Pmj
						} else {
							if(Pmj>define8){
								this.list[index]['Define4'] = Pmj
							}else{
								this.list[index]['Define4'] = define8
							}
						}
					}
				}
				//赋值完成票面价后计算票面价折扣
				let Pmjv = item['Define4']
				let Pmjzk = Pmjv / Cpbj * 100;
				this.list[index]['Define5'] = Pmjzk
			},
			
			
			// 是否可编辑状态
			initFiledState() {
				this.isfiledDisabled = {
					Define9: false, // 终端客户
					cInvCode: false, // 产品编号
					cInvName: true, // 产品名称
					Define1: false, // 产品报价
					Define2: false, // 申请结算价
					Define3: false, // 申请结算价折扣(%)
					Define4: false, // 申请票面价
					Define5: false, // 申请票面价折扣(%)
					Define6: true, // 原结算价
					Define8: false, // 原票面价
					Define10: true // 备注
				}
				if (this.bussiness.currentNodeid=="T10002") {
					this.$set(this.isfiledDisabled, 'cInvCode', true)
					this.$set(this.isfiledDisabled, 'Define1', true)
					this.$set(this.isfiledDisabled, 'Define4', true)
					this.$set(this.isfiledDisabled, 'Define5', true)
					this.$set(this.isfiledDisabled, 'Define8', true)
				} else {
					this.$set(this.isfiledDisabled, 'cInvCode', true)
					this.$set(this.isfiledDisabled, 'Define1', true)
					this.$set(this.isfiledDisabled, 'Define2', true)
					this.$set(this.isfiledDisabled, 'Define3', true)
					this.$set(this.isfiledDisabled, 'Define4', true)
					this.$set(this.isfiledDisabled, 'Define5', true)
					this.$set(this.isfiledDisabled, 'Define8', true)
					this.$set(this.isfiledDisabled, 'Define9', true)
				}
			},
			
			// 点击添加按钮调用方法
			async addItem() {
				let formData = this.formData
				if(!formData.cCusCode){
					uni.showToast({
						title: '【请选择客户！】',
						icon:'none'
					})
				}else{
					// 验证经销商是否已经分等级
					let cCusDefine6 = this.formData.cCusDefine6
					let Cust_TYPE = this.formData.Cust_TYPE
					let comCode1 = this.formData.comCode1
					if(cCusDefine6=="经销商"&&Cust_TYPE==""){
							alert("【此经销商没有分等级,不能申请价格！】");
							return false;
					}
					
					let comCode = "";
					if(comCode1=="北京销售-EBJS"){
							comCode = "UFDATA_015_2012";
					}else if(comCode1=="北京咨询-EBJC"){
							comCode = "UFDATA_021_2015";
					}else if(comCode1=="杭州销售-EHZS"){
							comCode = "UFDATA_030_2013";
					}else if(comCode1=="杭州检验所-EHZL"){
							comCode = "UFDATA_032_2014";
					}else if(comCode1=="天津销售-ETJS"){
							comCode = "UFDATA_031_2014";
					}
					// 判断所选择的客户是否为经销商以及经销商的等级
					let type = ''
					await ajax({
						url:'/bpm/post',
						method: 'POST',
						data: { ...this.searchField, ...{
							paramasMap: {
								comCode: comCode,
								cCusCode: formData.cCusCode
							},
							url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B162'
						} },
						success:res => {
							type = res.data.data
						}
					})
					this.$refs.productChoose.open()
				}
			},
			
			async del(item){
				let index = this.list.indexOf(item)
				let list = this.list.splice(index,1)
				let DocUnid = this.bussiness.docUnid
				let cInvCode = this.list.cInvCode
				console.log(list)
				// 删除数据
				await ajax({
					url:'/bpm/post',
					method: 'POST',
					data: { ...this.searchField, ...{
						paramasMap: {
							DocUnid,
							cInvCode
						},
						url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B160'
					} }
				})
				this.$emit('reload')
			},
			
			addproduct(item){
				this.$refs.productChoose.close()
				this.list.push({
					Define9: '', // 终端客户
					cInvCode: item.cInvCode, // 产品编号
					cInvName: item.cInvName, // 产品名称
					Define1: item.Price, // 产品报价
					Define2: '', // 申请结算价
					Define3: '', // 申请结算价折扣(%)
					Define4: '', // 申请票面价
					Define5: '', // 申请票面价折扣(%)
					Define6: '', // 原结算价
					Define8: '', // 原票面价
					Define10: '' // 备注
				})
			},
			
			async save(){
				let DocUnid = this.bussiness.docUnid
				// 保存数据
				await ajax({
					url:'/bpm/post',
					method: 'POST',
					data: { ...this.searchField, ...{
						paramasMap: {
							xmlValList: this.list,
							DocUnid
						},
						url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B161'
					} }
				})
				uni.setStorageSync('pro-lis',this.list)
				uni.navigateBack()
			}
		}
	}
</script>

<style scoped lang="scss">
	@import "@/common/css/style.css";
	@import "@/common/css/icons.css";
	.grace-nowrap{display:flex; flex-direction:row; flex-wrap:nowrap;}
	.grace-flex-vcenter{align-items:center;}
	.cancel{
		color: #d10000;
		position: absolute;
		top: 10rpx;
		right: 10rpx;
		.icon{
			font-size: 42rpx;
		}
	}
	.hd-title{
		width: 10rpx;height: 30rpx;background-color: #00B26A;margin-right: 5px;
	}
	.product {
		margin: 30rpx;
		font-size: 32rpx;
	}
	
	.product-content {
		position: relative;
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		padding: 30rpx;
		margin-bottom: 20rpx;
		border-radius: 10rpx;
		background-color: rgba(0, 167, 60, 0.19);
		line-height: 60rpx;
	}
	
	.product-title {
		font-weight: 500;
	}
	
	.left-margin {
		margin-left: 50rpx;
		color: #C0C0C0;
	}
	
	.content {
		font-size: 26rpx;
		font-weight: 500;
		padding-bottom: 46px;
	}
	
	.plan {
		padding: 30rpx;
		background-color: #FFFFFF;
	}
	
	.add {
		padding: 20rpx;
	}
	.btn-box{
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
	}
</style>
